//
//  MessageListCell.m
//  ProjectforHDX
//
//  Created by Jaesun on 16/7/1.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "MessageListCell.h"

@interface MessageListCell() {
   
    CGFloat detailHeight;
}


@end

@implementation MessageListCell

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self s_setupCellView];
        
    }
    return self;
}

/**
 *  初始化cell
 */
- (void)s_setupCellView {
    // 图片标志
    [self setupIconImgView];
    // 名称 标签
    [self setupTitleLabel];
    // 状态 标签
    [self setupstateImgView];
    // 内容 标签
    [self setupDetailLabel];
    // 时间 标签
    [self setupTimeLabel];
}

- (void)setModel:(MessageModel *)model {
    
    detailHeight = [self heightWithText:model.content];
    self.cellHeight = detailHeight + 55;
    
    UIImage *iconImg = BDFILEIMAGENAEM(@"mainVC/msgIcon");
    
    self.iconImgVeiw = [[RZCustomImageView alloc] initWithFrame:CGRectMake(10, self.cellHeight / 2 - iconImg.size.height / 2, iconImg.size.width, iconImg.size.height)];
   
    self.titleLabel.frame = CGRectMake(self.iconImgVeiw.right + 10, 15, [self widthForText:model.messageTitle], 15);
    self.titleLabel.text = model.messageTitle;
    
    UIImage *img = BDFILEIMAGENAEM(@"mainVC/new");
    self.stateImgView.frame = CGRectMake(self.titleLabel.right + 5, CGRectGetMidY(self.titleLabel.frame) - img.size.height / 2, img.size.width, img.size.height);
    if (1 == [model.isRead integerValue]) {
        self.stateImgView.hidden = YES;
    }
    self.detailLabel.text = model.content;
    
    self.detailLabel.frame = CGRectMake(self.titleLabel.left, self.titleLabel.bottom + 10, SCREEN_WIDTH - 50, detailHeight);
    
    self.timeLabel.text = [THMangerTool timeHandle:model.addTime dateFormatter:@"yyyy-MM-dd" ];
    
}

- (void)setupIconImgView {
    UIImage *iconImg = BDFILEIMAGENAEM(@"mainVC/msgIcon");
    
    self.iconImgVeiw = [[RZCustomImageView alloc] initWithFrame:CGRectMake(10, 75 / 2 - iconImg.size.height / 2, iconImg.size.width, iconImg.size.height)];
    self.iconImgVeiw.image = iconImg;
    [self.contentView addSubview:self.iconImgVeiw];

}

- (void)setupTitleLabel {
    
    // titleLabel
    self.titleLabel = [[RZCustomLable alloc] initWithFrame:CGRectMake(self.iconImgVeiw.right + 10, 15, 65, 15)];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.titleLabel.font = CustomFont(14);
    [self.contentView addSubview:self.titleLabel];
    
}

- (void)setupstateImgView {
    
    // stateImgView
    UIImage *img = BDFILEIMAGENAEM(@"mainVC/new");
    self.stateImgView = [[RZCustomImageView alloc] initWithFrame:CGRectMake(self.titleLabel.right + 5, CGRectGetMidY(self.titleLabel.frame) - img.size.height / 2, img.size.width, img.size.height)];
    self.stateImgView.image = img;
    [self.contentView addSubview:self.stateImgView];

}


- (void)setupTimeLabel {

    self.timeLabel = [[RZCustomLable alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 80, self.titleLabel.top, 70, self.titleLabel.height)];
    self.timeLabel.textAlignment = NSTextAlignmentRight;
    self.timeLabel.textColor = [UtilCheck hexStringToColor:@"868686" alpha:1];
    self.timeLabel.font = CustomFont(12);
    [self.contentView addSubview:self.timeLabel];
}

- (void)setupDetailLabel {
    
    self.detailLabel = [[RZCustomLable alloc] initWithFrame:CGRectMake(self.titleLabel.left, self.titleLabel.bottom + 10, SCREEN_WIDTH - 50, 0)];
    self.detailLabel.numberOfLines = 0;
    self.detailLabel.textColor = [UtilCheck hexStringToColor:@"868686" alpha:1];
    self.detailLabel.font = CustomFont(12);
    [self.contentView addSubview:self.detailLabel];
    
}

- (CGFloat)heightWithText:(NSString *)text {
    // 计算文字高度
    CGSize size = CGSizeMake(SCREEN_WIDTH - 50, 1000);
    NSDictionary *attributeDic = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
    CGRect rect = [text boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin) attributes:attributeDic context:nil];
    CGFloat textHeight = rect.size.height;
    return textHeight;
}

- (CGFloat)widthForText:(NSString *)text {
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
    return rect.size.width;
}


@end
